Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: extensions/browser/api/guest_view/web_view/web_view_internal_api.cc

Issue 1004253002: Enable <webview>.executeScript outside of Apps and Extensions [2] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Antony's comments Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h" 5 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h"
6 6
7 #include "base/strings/stringprintf.h" 7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "content/public/browser/browser_context.h"
9 #include "content/public/browser/render_process_host.h" 10 #include "content/public/browser/render_process_host.h"
10 #include "content/public/browser/render_view_host.h" 11 #include "content/public/browser/render_view_host.h"
11 #include "content/public/browser/storage_partition.h" 12 #include "content/public/browser/storage_partition.h"
12 #include "content/public/browser/web_contents.h" 13 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/stop_find_action.h" 14 #include "content/public/common/stop_find_action.h"
15 #include "content/public/common/url_fetcher.h"
14 #include "extensions/common/api/web_view_internal.h" 16 #include "extensions/common/api/web_view_internal.h"
17 #include "extensions/common/error_utils.h"
18 #include "net/base/load_flags.h"
19 #include "net/url_request/url_fetcher.h"
20 #include "net/url_request/url_fetcher_delegate.h"
15 #include "third_party/WebKit/public/web/WebFindOptions.h" 21 #include "third_party/WebKit/public/web/WebFindOptions.h"
16 22
17 using content::WebContents; 23 using content::WebContents;
18 using extensions::core_api::web_view_internal::SetPermission::Params; 24 using extensions::core_api::web_view_internal::SetPermission::Params;
19 using extensions::core_api::extension_types::InjectDetails; 25 using extensions::core_api::extension_types::InjectDetails;
26 using net::URLFetcher;
Devlin 2015/03/23 22:03:25 Nit: for the two places we use this, omit the "usi
Xi Han 2015/03/24 15:11:48 Actually, there are four places, but it is ok to o
20 namespace webview = extensions::core_api::web_view_internal; 27 namespace webview = extensions::core_api::web_view_internal;
21 28
22 namespace { 29 namespace {
23 30
24 const char kAppCacheKey[] = "appcache"; 31 const char kAppCacheKey[] = "appcache";
25 const char kCookiesKey[] = "cookies"; 32 const char kCookiesKey[] = "cookies";
26 const char kFileSystemsKey[] = "fileSystems"; 33 const char kFileSystemsKey[] = "fileSystems";
27 const char kIndexedDBKey[] = "indexedDB"; 34 const char kIndexedDBKey[] = "indexedDB";
28 const char kLocalStorageKey[] = "localStorage"; 35 const char kLocalStorageKey[] = "localStorage";
29 const char kWebSQLKey[] = "webSQL"; 36 const char kWebSQLKey[] = "webSQL";
30 const char kSinceKey[] = "since"; 37 const char kSinceKey[] = "since";
38 const char kBadFileEncodingError[] =
39 "Could not load file '*' for content script. It isn't UTF-8 encoded.";
40 const char kLoadFileError[] = "Failed to load file: \"*\". ";
31 41
32 int MaskForKey(const char* key) { 42 int MaskForKey(const char* key) {
33 if (strcmp(key, kAppCacheKey) == 0) 43 if (strcmp(key, kAppCacheKey) == 0)
34 return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE; 44 return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
35 if (strcmp(key, kCookiesKey) == 0) 45 if (strcmp(key, kCookiesKey) == 0)
36 return content::StoragePartition::REMOVE_DATA_MASK_COOKIES; 46 return content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
37 if (strcmp(key, kFileSystemsKey) == 0) 47 if (strcmp(key, kFileSystemsKey) == 0)
38 return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS; 48 return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
39 if (strcmp(key, kIndexedDBKey) == 0) 49 if (strcmp(key, kIndexedDBKey) == 0)
40 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB; 50 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
41 if (strcmp(key, kLocalStorageKey) == 0) 51 if (strcmp(key, kLocalStorageKey) == 0)
42 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; 52 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
43 if (strcmp(key, kWebSQLKey) == 0) 53 if (strcmp(key, kWebSQLKey) == 0)
44 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL; 54 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
45 return 0; 55 return 0;
46 } 56 }
47 57
48 } // namespace 58 } // namespace
49 59
50 namespace extensions { 60 namespace extensions {
51 61
62 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI.
63 // Each WebUIURLFetcher is associated with a given |render_process_id,
64 // render_view_id| pair.
65 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher
66 : public net::URLFetcherDelegate {
67 public:
68 WebUIURLFetcher(content::BrowserContext* context,
69 const GURL& url,
70 const ExecuteCodeFunction::WebUILoadFileCallback& callback)
71 : context_(context), url_(url), callback_(callback) {}
72 ~WebUIURLFetcher() override {}
73
74 void Start(int render_process_id, int render_view_id) {
75 fetcher_.reset(URLFetcher::Create(url_, URLFetcher::GET, this));
76 fetcher_->SetRequestContext(context_->GetRequestContext());
77 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
78
79 content::AssociateURLFetcherWithRenderFrame(
80 fetcher_.get(), url_, render_process_id, render_view_id);
81 fetcher_->Start();
82 }
83
84 private:
85 // net::URLFetcherDelegate:
86 void OnURLFetchComplete(const URLFetcher* source) override {
87 CHECK_EQ(fetcher_.get(), source);
88
89 std::string data;
90 bool result = false;
91 if (fetcher_->GetStatus().status() == net::URLRequestStatus::SUCCESS) {
92 result = fetcher_->GetResponseAsString(&data);
93 DCHECK(result);
94 }
95 fetcher_.reset();
96 callback_.Run(result, data);
97 }
98
99 content::BrowserContext* context_;
100 GURL url_;
101 const ExecuteCodeFunction::WebUILoadFileCallback callback_;
102 scoped_ptr<URLFetcher> fetcher_;
103
104 DISALLOW_COPY_AND_ASSIGN(WebUIURLFetcher);
105 };
106
52 bool WebViewInternalExtensionFunction::RunAsync() { 107 bool WebViewInternalExtensionFunction::RunAsync() {
53 int instance_id = 0; 108 int instance_id = 0;
54 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); 109 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
55 WebViewGuest* guest = WebViewGuest::From( 110 WebViewGuest* guest = WebViewGuest::From(
56 render_view_host()->GetProcess()->GetID(), instance_id); 111 render_view_host()->GetProcess()->GetID(), instance_id);
57 if (!guest) 112 if (!guest)
58 return false; 113 return false;
59 114
60 return RunAsyncSafe(guest); 115 return RunAsyncSafe(guest);
61 } 116 }
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 } 193 }
139 194
140 bool WebViewInternalExecuteCodeFunction::IsWebView() const { 195 bool WebViewInternalExecuteCodeFunction::IsWebView() const {
141 return true; 196 return true;
142 } 197 }
143 198
144 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const { 199 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const {
145 return guest_src_; 200 return guest_src_;
146 } 201 }
147 202
203 bool WebViewInternalExecuteCodeFunction::LoadFileForWebUI(
204 const std::string& file_src,
205 const WebUILoadFileCallback& callback) {
206 if (!render_view_host() || !render_view_host()->GetProcess())
207 return false;
208 WebViewGuest* guest = WebViewGuest::From(
209 render_view_host()->GetProcess()->GetID(), guest_instance_id_);
210 if (!guest || host_id().type() != HostID::WEBUI)
211 return false;
212
213 GURL owner_base_url(guest->GetOwnerSiteURL().GetWithEmptyPath());
214 GURL file_url(owner_base_url.Resolve(file_src));
215
216 url_fetcher_.reset(
217 new WebUIURLFetcher(this->browser_context(), file_url, callback));
218 url_fetcher_->Start(render_view_host()->GetProcess()->GetID(),
219 render_view_host()->GetRoutingID());
220 return true;
221 }
222
223 bool WebViewInternalExecuteCodeFunction::LoadFile(const std::string& file) {
224 if (!extension()) {
225 bool is_success = false;
226 is_success = LoadFileForWebUI(
227 *details_->file,
228 base::Bind(&WebViewInternalExecuteCodeFunction::DidLoadFileForWebUI,
229 this, file));
230 if (is_success)
231 return true;
232
233 SendResponse(false);
234 error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file);
235 return false;
236 }
237 return ExecuteCodeFunction::LoadFile(file);
238 }
239
240 void WebViewInternalExecuteCodeFunction::DidLoadFileForWebUI(
Devlin 2015/03/23 22:03:25 This doesn't really do anything different than Did
Xi Han 2015/03/24 15:11:48 They are slightly different with each other, since
241 const std::string& file,
242 bool success,
243 const std::string& data) {
244 if (success) {
245 if (!base::IsStringUTF8(data)) {
246 error_ = ErrorUtils::FormatErrorMessage(kBadFileEncodingError, file);
247 SendResponse(false);
248 } else if (!Execute(data))
249 SendResponse(false);
250 } else {
251 error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file);
252 SendResponse(false);
253 }
254 url_fetcher_.reset();
255 }
256
148 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { 257 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() {
149 } 258 }
150 259
151 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished( 260 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished(
152 const std::string& error, 261 const std::string& error,
153 const GURL& on_url, 262 const GURL& on_url,
154 const base::ListValue& result) { 263 const base::ListValue& result) {
155 if (error.empty()) 264 if (error.empty())
156 SetResult(result.DeepCopy()); 265 SetResult(result.DeepCopy());
157 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished( 266 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished(
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 // Will finish asynchronously. 631 // Will finish asynchronously.
523 return true; 632 return true;
524 } 633 }
525 634
526 void WebViewInternalClearDataFunction::ClearDataDone() { 635 void WebViewInternalClearDataFunction::ClearDataDone() {
527 Release(); // Balanced in RunAsync(). 636 Release(); // Balanced in RunAsync().
528 SendResponse(true); 637 SendResponse(true);
529 } 638 }
530 639
531 } // namespace extensions 640 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698