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
137
138
139
140
141
142
143
// Copyright 2008, 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/console/console.h"

#include "gears/base/common/message_service.h"
#include "gears/console/log_event.h"

DECLARE_DISPATCHER(GearsConsole);

const std::string GearsConsole::kModuleName("GearsConsole");

template<>
void Dispatcher<GearsConsole>::Init() {
RegisterMethod("log", &GearsConsole::Log);
RegisterProperty("onlog", &GearsConsole::GetOnLog, &GearsConsole::SetOnLog);
}

void GearsConsole::Log(JsCallContext *context) {
Initialize();

// Get and sanitize parameters.
std::string16 type_str;
std::string16 message;
JsArray args_array;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_STRING16, &type_str },
{ JSPARAM_REQUIRED, JSPARAM_STRING16, &message },
{ JSPARAM_OPTIONAL, JSPARAM_ARRAY, &args_array},
};
if (!context->GetArguments(ARRAYSIZE(argv), argv)) {
assert(context->is_exception_set());
return;
}

// Check input validity.
if (type_str.length() == 0) {
context->SetException(STRING16(L"type cannot be an empty string."));
return;
}

if (message.length() == 0) {
context->SetException(STRING16(L"message cannot be an empty string."));
return;
}

std::string16 msg = message;
if (argv[2].was_specified) {
InterpolateArgs(&message, &args_array);
}
LogEvent *log_event = new LogEvent(message, type_str, EnvPageLocationUrl());
MessageService::GetInstance()->NotifyObservers(observer_topic_.c_str(),
log_event);
}

void GearsConsole::GetOnLog(JsCallContext *context) {
JsRootedCallback *callback = callback_backend_->GetCallback();
if (callback == NULL) {
context->SetReturnValue(JSPARAM_NULL, 0);
} else {
context->SetReturnValue(JSPARAM_FUNCTION, callback);
}
}

void GearsConsole::SetOnLog(JsCallContext *context) {
Initialize();

// Get & sanitize parameters.
JsRootedCallback *function = NULL;
JsArgument argv[] = {
{ JSPARAM_OPTIONAL, JSPARAM_FUNCTION, &function },
};
context->GetArguments(ARRAYSIZE(argv), argv);
scoped_ptr<JsRootedCallback> scoped_function(function);

if (context->is_exception_set())
return;

callback_backend_->SetCallback(scoped_function.release());
}

void GearsConsole::Initialize() {
if (!callback_backend_.get()) {
observer_topic_ =
STRING16(L"console:logstream-") + EnvPageSecurityOrigin().url();
callback_backend_.reset(
new JsCallbackLoggingBackend(observer_topic_, GetJsRunner(), this));
}

// Create an event monitor to alert us when the page unloads.
if (!unload_monitor_.get()) {
unload_monitor_.reset(new JsEventMonitor(GetJsRunner(), JSEVENT_UNLOAD,
this));
}
}

// static
void GearsConsole::InterpolateArgs(std::string16 *message,
const JsArray *args) {
std::string16::size_type location = 0;
int args_length;
if (!args->GetLength(&args_length)) return;

for (int i = 0; i < args_length; i++) {
// Find the _next_ occurance of %s
location = message->find(STRING16(L"%s"), location);
if (location == std::string16::npos) break;

std::string16 string_value(STRING16(L"<Error converting to string>"));
args->GetElementAsStringWithCoercion(i, &string_value);

message->replace(location, 2, string_value);
location += string_value.size();
}
}

void GearsConsole::HandleEvent(JsEventType event_type) {
assert(event_type == JSEVENT_UNLOAD);
// TODO(nigeltao): do we really need to listen to the unload event!?
callback_backend_.reset();
}
Show details Hide details

Change log

r2831 by gears.daemon on Sep 10, 2008   Diff
[Author: nigeltao]

Move CreateModule<T> out of
base/xx/module_wrapper.h and into
base/common/base_class.h, unifying the
three CreateModule<T>
implementations, and allowing us to delete
the
base/common/module_wrapper.h file.

Also, eliminate the DECLARE_GEARS_WRAPPER
macro, since it doesn't
...
Go to: 
Project members, sign in to write a code review

Older revisions

r2820 by gears.daemon on Sep 09, 2008   Diff
[Author: nigeltao]

Push JsToken-manipulating functions to
be member methods of JsArray,
JsObject, and JsCallContext.
...
r2808 by gears.daemon on Sep 08, 2008   Diff
[Author: steveblock]

Updates callsites to use new version
of JsCallContext::GetArguments(). This
CL should introduce no change in
...
r1880 by gears.daemon on Jun 05, 2008   Diff
[Author: mpcomplete]

Fix crash when navigating away from a
page while a console log callback is
being called.
...
All revisions of this file

File info

Size: 5042 bytes, 143 lines

File properties

svn:executable
*