What's new? | Help | Directory | Sign in
Google
             
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2007, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "gears/base/npapi/browser_utils.h"

#include <stack>

#include "gears/base/common/browsing_context.h"
#include "gears/base/common/js_types.h"
#include "gears/base/common/string_utils.h"
#include "gears/base/common/thread_locals.h"
#include "gears/base/npapi/np_utils.h"
#include "gears/base/npapi/scoped_npapi_handles.h"

extern std::string16 g_user_agent; // Defined in base/npapi/npp_bindings.cc

typedef std::stack<JsCallContext*> JsCallStack;
const ThreadLocals::Slot kJsCallStackKey = ThreadLocals::Alloc();

static void DeleteJsCallStack(void *context) {
JsCallStack *call_stack = reinterpret_cast<JsCallStack*>(context);
delete call_stack;
}

static JsCallStack &GetJsCallStack() {
JsCallStack *call_stack =
reinterpret_cast<JsCallStack*>(ThreadLocals::GetValue(kJsCallStackKey));
if (!call_stack) {
call_stack = new JsCallStack;
ThreadLocals::SetValue(kJsCallStackKey, call_stack, &DeleteJsCallStack);
}
return *call_stack;
}

void BrowserUtils::EnterScope(JsCallContext *context) {
GetJsCallStack().push(context);
}

void BrowserUtils::ExitScope() {
assert(!GetJsCallStack().empty());
GetJsCallStack().pop();
}

void BrowserUtils::SetJsException(const std::string16& message) {
assert(!GetJsCallStack().empty());
GetJsCallStack().top()->SetException(message);
}

JsCallContext *BrowserUtils::GetCurrentJsCallContext() {
if (GetJsCallStack().empty())
return NULL;
return GetJsCallStack().top();
}

bool BrowserUtils::GetPageLocationUrl(JsContextPtr context,
std::string16 *location_url) {
assert(location_url);

// Retrieve window.location.href.
NPObject* window;
if (NPN_GetValue(context, NPNVWindowNPObject, &window) != NPERR_NO_ERROR)
return false;
ScopedNPObject window_scoped(window);

NPIdentifier location_id = NPN_GetStringIdentifier("location");
ScopedNPVariant np_location;
if (!NPN_GetProperty(context, window, location_id, &np_location) ||
!NPVARIANT_IS_OBJECT(np_location))
return false;

NPIdentifier href_id = NPN_GetStringIdentifier("href");
ScopedNPVariant np_href;
if (!NPN_GetProperty(context, NPVARIANT_TO_OBJECT(np_location),
href_id, &np_href) ||
!NPVARIANT_IS_STRING(np_href)) {
return false;
}

NPString np_str = NPVARIANT_TO_STRING(np_href);
if (np_str.UTF8Length == 0)
return false;

return (UTF8ToString16(GetNPStringUTF8Characters(np_str),
GetNPStringUTF8Length(np_str),
location_url));
}

bool BrowserUtils::GetUserAgentString(std::string16 *user_agent) {
if (g_user_agent.empty()) {
return false; // Not initialized yet.
}
*user_agent = g_user_agent;
return true;
}

bool BrowserUtils::GetPageSecurityOrigin(JsContextPtr context,
SecurityOrigin *security_origin) {
std::string16 location;
if (!GetPageLocationUrl(context, &location))
return false;
return security_origin->InitFromUrl(location.c_str());
}

bool BrowserUtils::GetPageBrowsingContext(
JsContextPtr context, scoped_refptr<BrowsingContext> *browsing_context) {
// TODO(mpcomplete): implement me.
browsing_context->reset();
return true;
}

bool BrowserUtils::IsOnline() {
// TODO(mpcomplete): implement me.
return true;
}
Show details Hide details

Change log

r2457 by gears.daemon on Jul 22, 2008   Diff
[Author: mpcomplete]

Attempt to fix NULL-pointer dereference
from BrowserUtils::GetPageLocationUrl
calling UTF8ToString16.

BUG=1283091
R=playmobil
CC=gears-internal
DELTA=16  (8 added, 4 deleted, 4 changed)
OCL=7777840
SCL=7778384
Go to: 
Project members, sign in to write a code review

Older revisions

r2117 by gears.daemon on Jun 25, 2008   Diff
[Author: mpcomplete]

ThreadLocals optimizations:
- Change the key from a string to an
int.
...
r1814 by gears.daemon on May 30, 2008   Diff
[Author: mpcomplete]

Add BrowsingContext to associate with
network requests.

...
r1427 by gears.daemon on Apr 18, 2008   Diff
[Author: mpcomplete]

Workaround for NPAPI issue where
exceptions thrown inside callbacks
wouldn't bubble up to Gears.  This
...
All revisions of this file

File info

Size: 4814 bytes, 136 lines