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

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: nits 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
« no previous file with comments | « extensions/browser/api/guest_view/web_view/web_view_internal_api.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
20 namespace webview = extensions::core_api::web_view_internal; 26 namespace webview = extensions::core_api::web_view_internal;
21 27
22 namespace { 28 namespace {
23 29
24 const char kAppCacheKey[] = "appcache"; 30 const char kAppCacheKey[] = "appcache";
25 const char kCookiesKey[] = "cookies"; 31 const char kCookiesKey[] = "cookies";
26 const char kFileSystemsKey[] = "fileSystems"; 32 const char kFileSystemsKey[] = "fileSystems";
27 const char kIndexedDBKey[] = "indexedDB"; 33 const char kIndexedDBKey[] = "indexedDB";
28 const char kLocalStorageKey[] = "localStorage"; 34 const char kLocalStorageKey[] = "localStorage";
29 const char kWebSQLKey[] = "webSQL"; 35 const char kWebSQLKey[] = "webSQL";
30 const char kSinceKey[] = "since"; 36 const char kSinceKey[] = "since";
37 const char kLoadFileError[] = "Failed to load file: \"*\". ";
31 38
32 int MaskForKey(const char* key) { 39 int MaskForKey(const char* key) {
33 if (strcmp(key, kAppCacheKey) == 0) 40 if (strcmp(key, kAppCacheKey) == 0)
34 return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE; 41 return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
35 if (strcmp(key, kCookiesKey) == 0) 42 if (strcmp(key, kCookiesKey) == 0)
36 return content::StoragePartition::REMOVE_DATA_MASK_COOKIES; 43 return content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
37 if (strcmp(key, kFileSystemsKey) == 0) 44 if (strcmp(key, kFileSystemsKey) == 0)
38 return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS; 45 return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
39 if (strcmp(key, kIndexedDBKey) == 0) 46 if (strcmp(key, kIndexedDBKey) == 0)
40 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB; 47 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
41 if (strcmp(key, kLocalStorageKey) == 0) 48 if (strcmp(key, kLocalStorageKey) == 0)
42 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; 49 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
43 if (strcmp(key, kWebSQLKey) == 0) 50 if (strcmp(key, kWebSQLKey) == 0)
44 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL; 51 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
45 return 0; 52 return 0;
46 } 53 }
47 54
48 } // namespace 55 } // namespace
49 56
50 namespace extensions { 57 namespace extensions {
51 58
59 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI.
60 // Each WebUIURLFetcher is associated with a given |render_process_id,
61 // render_view_id| pair.
62 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher
63 : public net::URLFetcherDelegate {
64 public:
65 WebUIURLFetcher(
66 content::BrowserContext* context,
67 const WebViewInternalExecuteCodeFunction::WebUILoadFileCallback& callback)
68 : context_(context), callback_(callback) {}
69 ~WebUIURLFetcher() override {}
70
71 void Start(int render_process_id, int render_view_id, const GURL& url) {
72 fetcher_.reset(net::URLFetcher::Create(url, net::URLFetcher::GET, this));
73 fetcher_->SetRequestContext(context_->GetRequestContext());
74 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
75
76 content::AssociateURLFetcherWithRenderFrame(
77 fetcher_.get(), url, render_process_id, render_view_id);
78 fetcher_->Start();
79 }
80
81 private:
82 // net::URLFetcherDelegate:
83 void OnURLFetchComplete(const net::URLFetcher* source) override {
84 CHECK_EQ(fetcher_.get(), source);
85
86 std::string data;
87 bool result = false;
88 if (fetcher_->GetStatus().status() == net::URLRequestStatus::SUCCESS) {
89 result = fetcher_->GetResponseAsString(&data);
90 DCHECK(result);
91 }
92 fetcher_.reset();
93 callback_.Run(result, data);
94 callback_.Reset();
95 }
96
97 content::BrowserContext* context_;
98 WebViewInternalExecuteCodeFunction::WebUILoadFileCallback callback_;
99 scoped_ptr<net::URLFetcher> fetcher_;
100
101 DISALLOW_COPY_AND_ASSIGN(WebUIURLFetcher);
102 };
103
52 bool WebViewInternalExtensionFunction::RunAsync() { 104 bool WebViewInternalExtensionFunction::RunAsync() {
53 int instance_id = 0; 105 int instance_id = 0;
54 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); 106 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
55 WebViewGuest* guest = WebViewGuest::From( 107 WebViewGuest* guest = WebViewGuest::From(
56 render_view_host()->GetProcess()->GetID(), instance_id); 108 render_view_host()->GetProcess()->GetID(), instance_id);
57 if (!guest) 109 if (!guest)
58 return false; 110 return false;
59 111
60 return RunAsyncSafe(guest); 112 return RunAsyncSafe(guest);
61 } 113 }
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 } 190 }
139 191
140 bool WebViewInternalExecuteCodeFunction::IsWebView() const { 192 bool WebViewInternalExecuteCodeFunction::IsWebView() const {
141 return true; 193 return true;
142 } 194 }
143 195
144 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const { 196 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const {
145 return guest_src_; 197 return guest_src_;
146 } 198 }
147 199
200 bool WebViewInternalExecuteCodeFunction::LoadFileForWebUI(
201 const std::string& file_src,
202 const WebUILoadFileCallback& callback) {
203 if (!render_view_host() || !render_view_host()->GetProcess())
204 return false;
205 WebViewGuest* guest = WebViewGuest::From(
206 render_view_host()->GetProcess()->GetID(), guest_instance_id_);
207 if (!guest || host_id().type() != HostID::WEBUI)
208 return false;
209
210 GURL owner_base_url(guest->GetOwnerSiteURL().GetWithEmptyPath());
211 GURL file_url(owner_base_url.Resolve(file_src));
212
213 url_fetcher_.reset(new WebUIURLFetcher(this->browser_context(), callback));
214 url_fetcher_->Start(render_view_host()->GetProcess()->GetID(),
215 render_view_host()->GetRoutingID(), file_url);
216 return true;
217 }
218
219 bool WebViewInternalExecuteCodeFunction::LoadFile(const std::string& file) {
220 if (!extension()) {
221 if (LoadFileForWebUI(
222 *details_->file,
223 base::Bind(
224 &WebViewInternalExecuteCodeFunction::DidLoadAndLocalizeFile,
225 this, file)))
226 return true;
227
228 SendResponse(false);
229 error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file);
230 return false;
231 }
232 return ExecuteCodeFunction::LoadFile(file);
233 }
234
148 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { 235 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() {
149 } 236 }
150 237
151 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished( 238 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished(
152 const std::string& error, 239 const std::string& error,
153 const GURL& on_url, 240 const GURL& on_url,
154 const base::ListValue& result) { 241 const base::ListValue& result) {
155 if (error.empty()) 242 if (error.empty())
156 SetResult(result.DeepCopy()); 243 SetResult(result.DeepCopy());
157 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished( 244 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished(
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 // Will finish asynchronously. 609 // Will finish asynchronously.
523 return true; 610 return true;
524 } 611 }
525 612
526 void WebViewInternalClearDataFunction::ClearDataDone() { 613 void WebViewInternalClearDataFunction::ClearDataDone() {
527 Release(); // Balanced in RunAsync(). 614 Release(); // Balanced in RunAsync().
528 SendResponse(true); 615 SendResponse(true);
529 } 616 }
530 617
531 } // namespace extensions 618 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/guest_view/web_view/web_view_internal_api.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698