OLD | NEW |
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 Loading... |
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(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, false, data); |
| 97 callback_.Run(false, data); |
| 98 return; |
| 99 } |
| 100 |
| 101 bool result = fetcher->GetResponseAsString(&data); |
| 102 DCHECK(result) << "Invalid fethcer setting."; |
| 103 callback_.Run(true, data); |
| 104 } |
| 105 |
| 106 WebViewInternalExecuteCodeFunction* function_; |
| 107 const 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 Loading... |
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 WebUIURLFetcher* fetcher = new WebUIURLFetcher( |
| 226 this, file_url, render_view_host()->GetProcess()->GetID(), |
| 227 render_view_host()->GetRoutingID(), callback); |
| 228 fetchers_.push_back(fetcher); |
| 229 fetcher->Start(); |
| 230 return true; |
| 231 } |
| 232 |
| 233 void WebViewInternalExecuteCodeFunction::OnWebUIURLFetchComplete( |
| 234 const WebUIURLFetcher* fetcher, |
| 235 bool is_success, |
| 236 std::string data) { |
| 237 fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher)); |
| 238 } |
| 239 |
148 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { | 240 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { |
149 } | 241 } |
150 | 242 |
151 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished( | 243 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished( |
152 const std::string& error, | 244 const std::string& error, |
153 const GURL& on_url, | 245 const GURL& on_url, |
154 const base::ListValue& result) { | 246 const base::ListValue& result) { |
155 if (error.empty()) | 247 if (error.empty()) |
156 SetResult(result.DeepCopy()); | 248 SetResult(result.DeepCopy()); |
157 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished( | 249 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished( |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 // Will finish asynchronously. | 614 // Will finish asynchronously. |
523 return true; | 615 return true; |
524 } | 616 } |
525 | 617 |
526 void WebViewInternalClearDataFunction::ClearDataDone() { | 618 void WebViewInternalClearDataFunction::ClearDataDone() { |
527 Release(); // Balanced in RunAsync(). | 619 Release(); // Balanced in RunAsync(). |
528 SendResponse(true); | 620 SendResponse(true); |
529 } | 621 } |
530 | 622 |
531 } // namespace extensions | 623 } // namespace extensions |
OLD | NEW |