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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/renderer/extensions/webstore_bindings.h"
#include "base/strings/string_util.h"
#include "chrome/common/extensions/api/webstore/webstore_api_constants.h"
#include "chrome/common/extensions/chrome_extension_messages.h"
#include "chrome/common/extensions/extension_constants.h"
#include "content/public/renderer/render_view.h"
#include "extensions/common/extension.h"
#include "extensions/renderer/script_context.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebNode.h"
#include "third_party/WebKit/public/web/WebNodeList.h"
#include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
#include "url/gurl.h"
#include "v8/include/v8.h"
using blink::WebDocument;
using blink::WebElement;
using blink::WebFrame;
using blink::WebNode;
using blink::WebNodeList;
using blink::WebUserGestureIndicator;
namespace extensions {
namespace {
const char kWebstoreLinkRelation[] = "chrome-webstore-item";
const char kNotInTopFrameError[] =
"Chrome Web Store installations can only be started by the top frame.";
const char kNotUserGestureError[] =
"Chrome Web Store installations can only be initated by a user gesture.";
const char kNoWebstoreItemLinkFoundError[] =
"No Chrome Web Store item link found.";
const char kInvalidWebstoreItemUrlError[] =
"Invalid Chrome Web Store item URL.";
// chrome.webstore.install() calls generate an install ID so that the install's
// callbacks may be fired when the browser notifies us of install completion
// (successful or not) via OnInlineWebstoreInstallResponse.
int g_next_install_id = 0;
} // anonymous namespace
WebstoreBindings::WebstoreBindings(ScriptContext* context)
: ObjectBackedNativeHandler(context), ChromeV8ExtensionHandler(context) {
RouteFunction("Install",
base::Bind(&WebstoreBindings::Install, base::Unretained(this)));
}
void WebstoreBindings::Install(
const v8::FunctionCallbackInfo<v8::Value>& args) {
content::RenderView* render_view = context()->GetRenderView();
if (!render_view)
return;
// The first two arguments indicate whether or not there are install stage
// or download progress listeners.
int listener_mask = 0;
CHECK(args[0]->IsBoolean());
if (args[0]->BooleanValue())
listener_mask |= api::webstore::INSTALL_STAGE_LISTENER;
CHECK(args[1]->IsBoolean());
if (args[1]->BooleanValue())
listener_mask |= api::webstore::DOWNLOAD_PROGRESS_LISTENER;
std::string preferred_store_link_url;
if (!args[2]->IsUndefined()) {
CHECK(args[2]->IsString());
preferred_store_link_url = std::string(*v8::String::Utf8Value(args[2]));
}
std::string webstore_item_id;
std::string error;
WebFrame* frame = context()->web_frame();
if (!GetWebstoreItemIdFromFrame(
frame, preferred_store_link_url, &webstore_item_id, &error)) {
args.GetIsolate()->ThrowException(
v8::String::NewFromUtf8(args.GetIsolate(), error.c_str()));
return;
}
int install_id = g_next_install_id++;
Send(new ExtensionHostMsg_InlineWebstoreInstall(render_view->GetRoutingID(),
install_id,
GetRoutingID(),
webstore_item_id,
frame->document().url(),
listener_mask));
args.GetReturnValue().Set(static_cast<int32_t>(install_id));
}
// static
bool WebstoreBindings::GetWebstoreItemIdFromFrame(
WebFrame* frame, const std::string& preferred_store_link_url,
std::string* webstore_item_id, std::string* error) {
if (frame != frame->top()) {
*error = kNotInTopFrameError;
return false;
}
if (!WebUserGestureIndicator::isProcessingUserGesture()) {
*error = kNotUserGestureError;
return false;
}
WebDocument document = frame->document();
if (document.isNull()) {
*error = kNoWebstoreItemLinkFoundError;
return false;
}
WebElement head = document.head();
if (head.isNull()) {
*error = kNoWebstoreItemLinkFoundError;
return false;
}
GURL webstore_base_url =
GURL(extension_urls::GetWebstoreItemDetailURLPrefix());
WebNodeList children = head.childNodes();
for (unsigned i = 0; i < children.length(); ++i) {
WebNode child = children.item(i);
if (!child.isElementNode())
continue;
WebElement elem = child.to<WebElement>();
if (!elem.hasHTMLTagName("link") || !elem.hasAttribute("rel") ||
!elem.hasAttribute("href"))
continue;
std::string rel = elem.getAttribute("rel").utf8();
if (!LowerCaseEqualsASCII(rel, kWebstoreLinkRelation))
continue;
std::string webstore_url_string(elem.getAttribute("href").utf8());
if (!preferred_store_link_url.empty() &&
preferred_store_link_url != webstore_url_string) {
continue;
}
GURL webstore_url = GURL(webstore_url_string);
if (!webstore_url.is_valid()) {
*error = kInvalidWebstoreItemUrlError;
return false;
}
if (webstore_url.scheme() != webstore_base_url.scheme() ||
webstore_url.host() != webstore_base_url.host() ||
!StartsWithASCII(
webstore_url.path(), webstore_base_url.path(), true)) {
*error = kInvalidWebstoreItemUrlError;
return false;
}
std::string candidate_webstore_item_id = webstore_url.path().substr(
webstore_base_url.path().length());
if (!extensions::Extension::IdIsValid(candidate_webstore_item_id)) {
*error = kInvalidWebstoreItemUrlError;
return false;
}
std::string reconstructed_webstore_item_url_string =
extension_urls::GetWebstoreItemDetailURLPrefix() +
candidate_webstore_item_id;
if (reconstructed_webstore_item_url_string != webstore_url_string) {
*error = kInvalidWebstoreItemUrlError;
return false;
}
*webstore_item_id = candidate_webstore_item_id;
return true;
}
*error = kNoWebstoreItemLinkFoundError;
return false;
}
bool WebstoreBindings::OnMessageReceived(const IPC::Message& message) {
IPC_BEGIN_MESSAGE_MAP(WebstoreBindings, message)
IPC_MESSAGE_HANDLER(ExtensionMsg_InlineWebstoreInstallResponse,
OnInlineWebstoreInstallResponse)
IPC_MESSAGE_HANDLER(ExtensionMsg_InlineInstallStageChanged,
OnInlineInstallStageChanged)
IPC_MESSAGE_HANDLER(ExtensionMsg_InlineInstallDownloadProgress,
OnInlineInstallDownloadProgress)
IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message")
IPC_END_MESSAGE_MAP()
return true;
}
void WebstoreBindings::OnInlineWebstoreInstallResponse(
int install_id,
bool success,
const std::string& error,
webstore_install::Result result) {
v8::Isolate* isolate = context()->isolate();
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(context()->v8_context());
v8::Handle<v8::Value> argv[] = {
v8::Integer::New(isolate, install_id),
v8::Boolean::New(isolate, success),
v8::String::NewFromUtf8(isolate, error.c_str()),
v8::String::NewFromUtf8(
isolate, api::webstore::kInstallResultCodes[static_cast<int>(result)])
};
context()->module_system()->CallModuleMethod(
"webstore", "onInstallResponse", arraysize(argv), argv);
}
void WebstoreBindings::OnInlineInstallStageChanged(int stage) {
const char* stage_string = NULL;
api::webstore::InstallStage install_stage =
static_cast<api::webstore::InstallStage>(stage);
switch (install_stage) {
case api::webstore::INSTALL_STAGE_DOWNLOADING:
stage_string = api::webstore::kInstallStageDownloading;
break;
case api::webstore::INSTALL_STAGE_INSTALLING:
stage_string = api::webstore::kInstallStageInstalling;
break;
}
v8::Isolate* isolate = context()->isolate();
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(context()->v8_context());
v8::Handle<v8::Value> argv[] = {
v8::String::NewFromUtf8(isolate, stage_string)};
context()->module_system()->CallModuleMethod(
"webstore", "onInstallStageChanged", arraysize(argv), argv);
}
void WebstoreBindings::OnInlineInstallDownloadProgress(int percent_downloaded) {
v8::Isolate* isolate = context()->isolate();
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(context()->v8_context());
v8::Handle<v8::Value> argv[] = {
v8::Number::New(isolate, percent_downloaded / 100.0)};
context()->module_system()->CallModuleMethod(
"webstore", "onDownloadProgress", arraysize(argv), argv);
}
} // namespace extensions
|