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

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: Add a test. 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 "net/base/load_flags.h"
18 #include "net/url_request/url_fetcher.h"
19 #include "net/url_request/url_fetcher_delegate.h"
15 #include "third_party/WebKit/public/web/WebFindOptions.h" 20 #include "third_party/WebKit/public/web/WebFindOptions.h"
16 21
17 using content::WebContents; 22 using content::WebContents;
18 using extensions::core_api::web_view_internal::SetPermission::Params; 23 using extensions::core_api::web_view_internal::SetPermission::Params;
19 using extensions::core_api::extension_types::InjectDetails; 24 using extensions::core_api::extension_types::InjectDetails;
25 using net::URLFetcher;
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";
(...skipping 12 matching lines...) Expand all
42 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; 48 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
43 if (strcmp(key, kWebSQLKey) == 0) 49 if (strcmp(key, kWebSQLKey) == 0)
44 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL; 50 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
45 return 0; 51 return 0;
46 } 52 }
47 53
48 } // namespace 54 } // namespace
49 55
50 namespace extensions { 56 namespace extensions {
51 57
58 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI.
59 // Each WebUIURLFetcher is associated with a given |render_process_id,
60 // render_view_id| pair.
61 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher
62 : public net::URLFetcherDelegate {
63 public:
64 WebUIURLFetcher(scoped_refptr<WebViewInternalExecuteCodeFunction> function,
65 const GURL& url,
66 int render_process_id,
67 int render_view_id,
68 const ExecuteCodeFunction::WebUILoadFileCallback& callback)
69 : function_(function),
70 url_(url),
71 render_process_id_(render_process_id),
72 render_view_id_(render_view_id),
73 callback_(callback) {}
74 ~WebUIURLFetcher() override {}
75
76 void Start() {
77 fetcher_.reset(URLFetcher::Create(url_, URLFetcher::GET, this));
78 fetcher_->SetRequestContext(
79 function_->browser_context()->GetRequestContext());
80 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
81
82 content::AssociateURLFetcherWithRenderFrame(
83 fetcher_.get(), url_, render_process_id_, render_view_id_);
84 fetcher_->Start();
85 }
86
87 private:
88 // net::URLFetcherDelegate:
89 void OnURLFetchComplete(const URLFetcher* source) override {
90 CHECK_EQ(fetcher_.get(), source);
91 scoped_ptr<net::URLFetcher> fetcher(fetcher_.Pass());
92
93 std::string data;
94 if (fetcher->GetStatus().status() != net::URLRequestStatus::SUCCESS ||
95 fetcher->GetResponseCode() != 200) {
96 function_->OnWebUIURLFetchComplete(this);
Devlin 2015/03/18 16:52:27 This strikes me as very odd. You both call direct
Xi Han 2015/03/18 20:36:26 Remove the call to the function here.
97 callback_.Run(false, data);
98 return;
99 }
100
101 bool result = fetcher->GetResponseAsString(&data);
102 DCHECK(result) << "Invalid fetcher setting.";
103 callback_.Run(true, data);
104 }
105
106 scoped_refptr<WebViewInternalExecuteCodeFunction> function_;
107 GURL url_;
108 const int render_process_id_;
109 const int render_view_id_;
110 const ExecuteCodeFunction::WebUILoadFileCallback callback_;
111 scoped_ptr<URLFetcher> fetcher_;
112
113 DISALLOW_COPY_AND_ASSIGN(WebUIURLFetcher);
114 };
115
52 bool WebViewInternalExtensionFunction::RunAsync() { 116 bool WebViewInternalExtensionFunction::RunAsync() {
53 int instance_id = 0; 117 int instance_id = 0;
54 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); 118 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
55 WebViewGuest* guest = WebViewGuest::From( 119 WebViewGuest* guest = WebViewGuest::From(
56 render_view_host()->GetProcess()->GetID(), instance_id); 120 render_view_host()->GetProcess()->GetID(), instance_id);
57 if (!guest) 121 if (!guest)
58 return false; 122 return false;
59 123
60 return RunAsyncSafe(guest); 124 return RunAsyncSafe(guest);
61 } 125 }
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 } 202 }
139 203
140 bool WebViewInternalExecuteCodeFunction::IsWebView() const { 204 bool WebViewInternalExecuteCodeFunction::IsWebView() const {
141 return true; 205 return true;
142 } 206 }
143 207
144 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const { 208 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const {
145 return guest_src_; 209 return guest_src_;
146 } 210 }
147 211
212 bool WebViewInternalExecuteCodeFunction::LoadFileForWebUI(
213 const std::string& file_src,
214 const WebUILoadFileCallback& callback) {
215 if (!render_view_host() || !render_view_host()->GetProcess())
216 return false;
217 WebViewGuest* guest = WebViewGuest::From(
218 render_view_host()->GetProcess()->GetID(), guest_instance_id_);
219 if (!guest || host_id().type() != HostID::WEBUI)
220 return false;
221
222 GURL owner_base_url(guest->GetOwnerSiteURL().GetWithEmptyPath());
223 GURL file_url(owner_base_url.Resolve(file_src));
224
225 scoped_ptr<WebUIURLFetcher> fetcher(
226 new WebUIURLFetcher(make_scoped_refptr(this), file_url,
227 render_view_host()->GetProcess()->GetID(),
228 render_view_host()->GetRoutingID(), callback));
229 fetcher->Start();
230 fetchers_.push_back(fetcher.release()); // Pass ownership to |fetchers|.
231 return true;
232 }
233
234 void WebViewInternalExecuteCodeFunction::OnWebUIURLFetchComplete(
235 const WebUIURLFetcher* fetcher) {
236 fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher));
Devlin 2015/03/18 16:52:27 When do we ever have multiple fetchers? Can't you
Xi Han 2015/03/18 20:36:26 Hmmm, we need to store the fetcher in somewhere. T
Devlin 2015/03/19 15:45:35 Right; I wasn't saying we don't need to store it,
237 }
238
148 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { 239 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() {
149 } 240 }
150 241
151 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished( 242 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished(
152 const std::string& error, 243 const std::string& error,
153 const GURL& on_url, 244 const GURL& on_url,
154 const base::ListValue& result) { 245 const base::ListValue& result) {
155 if (error.empty()) 246 if (error.empty())
156 SetResult(result.DeepCopy()); 247 SetResult(result.DeepCopy());
157 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished( 248 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished(
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 // Will finish asynchronously. 613 // Will finish asynchronously.
523 return true; 614 return true;
524 } 615 }
525 616
526 void WebViewInternalClearDataFunction::ClearDataDone() { 617 void WebViewInternalClearDataFunction::ClearDataDone() {
527 Release(); // Balanced in RunAsync(). 618 Release(); // Balanced in RunAsync().
528 SendResponse(true); 619 SendResponse(true);
529 } 620 }
530 621
531 } // namespace extensions 622 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698