Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/page_capture/page_capture_api.h" | 5 #include "chrome/browser/extensions/api/page_capture/page_capture_api.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <memory> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | |
| 11 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 12 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/extensions/extension_tab_util.h" | 14 #include "chrome/browser/extensions/extension_tab_util.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/pref_names.h" | |
| 17 #include "chromeos/login/login_state.h" | |
| 18 #include "components/prefs/scoped_user_pref_update.h" | |
| 15 #include "content/public/browser/child_process_security_policy.h" | 19 #include "content/public/browser/child_process_security_policy.h" |
| 16 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
| 17 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
| 18 #include "content/public/browser/notification_types.h" | 22 #include "content/public/browser/notification_types.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 23 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/common/mhtml_generation_params.h" | 26 #include "content/public/common/mhtml_generation_params.h" |
| 23 #include "extensions/common/extension_messages.h" | 27 #include "extensions/common/extension_messages.h" |
| 28 #include "extensions/common/permissions/manifest_permission_set.h" | |
| 29 #include "extensions/common/permissions/permission_set.h" | |
| 30 #include "extensions/common/url_pattern_set.h" | |
| 24 | 31 |
| 25 using content::BrowserThread; | 32 using content::BrowserThread; |
| 26 using content::ChildProcessSecurityPolicy; | 33 using content::ChildProcessSecurityPolicy; |
| 27 using content::WebContents; | 34 using content::WebContents; |
| 28 using extensions::PageCaptureSaveAsMHTMLFunction; | 35 using extensions::PageCaptureSaveAsMHTMLFunction; |
| 29 using storage::ShareableFileReference; | 36 using storage::ShareableFileReference; |
| 30 | 37 |
| 31 namespace SaveAsMHTML = extensions::api::page_capture::SaveAsMHTML; | 38 namespace SaveAsMHTML = extensions::api::page_capture::SaveAsMHTML; |
| 32 | 39 |
| 33 namespace { | 40 namespace { |
| 34 | 41 |
| 35 const char kFileTooBigError[] = "The MHTML file generated is too big."; | 42 const char kFileTooBigError[] = "The MHTML file generated is too big."; |
| 36 const char kMHTMLGenerationFailedError[] = "Failed to generate MHTML."; | 43 const char kMHTMLGenerationFailedError[] = "Failed to generate MHTML."; |
| 37 const char kTemporaryFileError[] = "Failed to create a temporary file."; | 44 const char kTemporaryFileError[] = "Failed to create a temporary file."; |
| 38 const char kTabClosedError[] = "Cannot find the tab for this request."; | 45 const char kTabClosedError[] = "Cannot find the tab for this request."; |
| 39 | 46 |
| 47 #if defined(OS_CHROMEOS) | |
| 48 const char kUserDenied[] = "User denied request."; | |
| 49 #endif | |
| 50 | |
| 51 bool IsPublicSession() { | |
| 52 #if defined(OS_CHROMEOS) | |
| 53 if (chromeos::LoginState::IsInitialized()) | |
| 54 return chromeos::LoginState::Get()->IsPublicSessionUser(); | |
| 55 #endif | |
| 56 return false; | |
| 57 } | |
| 58 | |
| 40 } // namespace | 59 } // namespace |
| 41 | 60 |
| 42 static PageCaptureSaveAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; | 61 static PageCaptureSaveAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; |
| 43 | 62 |
| 44 PageCaptureSaveAsMHTMLFunction::PageCaptureSaveAsMHTMLFunction() { | 63 PageCaptureSaveAsMHTMLFunction::PageCaptureSaveAsMHTMLFunction() { |
| 45 } | 64 } |
| 46 | 65 |
| 47 PageCaptureSaveAsMHTMLFunction::~PageCaptureSaveAsMHTMLFunction() { | 66 PageCaptureSaveAsMHTMLFunction::~PageCaptureSaveAsMHTMLFunction() { |
| 48 if (mhtml_file_.get()) { | 67 if (mhtml_file_.get()) { |
| 49 storage::ShareableFileReference* to_release = mhtml_file_.get(); | 68 storage::ShareableFileReference* to_release = mhtml_file_.get(); |
| 50 to_release->AddRef(); | 69 to_release->AddRef(); |
| 51 mhtml_file_ = NULL; | 70 mhtml_file_ = NULL; |
| 52 BrowserThread::ReleaseSoon(BrowserThread::IO, FROM_HERE, to_release); | 71 BrowserThread::ReleaseSoon(BrowserThread::IO, FROM_HERE, to_release); |
| 53 } | 72 } |
| 54 } | 73 } |
| 55 | 74 |
| 56 void PageCaptureSaveAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { | 75 void PageCaptureSaveAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { |
| 57 test_delegate_ = delegate; | 76 test_delegate_ = delegate; |
| 58 } | 77 } |
| 59 | 78 |
| 60 bool PageCaptureSaveAsMHTMLFunction::RunAsync() { | 79 bool PageCaptureSaveAsMHTMLFunction::RunAsync() { |
| 61 params_ = SaveAsMHTML::Params::Create(*args_); | 80 params_ = SaveAsMHTML::Params::Create(*args_); |
| 62 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 81 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 63 | 82 |
| 64 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() | 83 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() |
| 65 | 84 |
| 85 // In Public Sessions, extensions (and apps) are force-installed by admin | |
| 86 // policy so the user does not get a chance to review the permissions for | |
| 87 // these extensions. This is not acceptable from a security/privacy | |
| 88 // standpoint, so when an extension uses the PageCapture API for the first | |
| 89 // time, we show the user a dialog where they can choose whether to allow the | |
| 90 // extension access to the API. | |
| 91 if (IsPublicSession()) { | |
| 92 HandlePermission(); | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 66 BrowserThread::PostTask( | 96 BrowserThread::PostTask( |
| 67 BrowserThread::FILE, FROM_HERE, | 97 BrowserThread::FILE, FROM_HERE, |
| 68 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); | 98 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); |
| 69 return true; | 99 return true; |
| 70 } | 100 } |
| 71 | 101 |
| 72 bool PageCaptureSaveAsMHTMLFunction::OnMessageReceived( | 102 bool PageCaptureSaveAsMHTMLFunction::OnMessageReceived( |
| 73 const IPC::Message& message) { | 103 const IPC::Message& message) { |
| 74 if (message.type() != ExtensionHostMsg_ResponseAck::ID) | 104 if (message.type() != ExtensionHostMsg_ResponseAck::ID) |
| 75 return false; | 105 return false; |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 GetProfile(), | 223 GetProfile(), |
| 194 include_incognito(), | 224 include_incognito(), |
| 195 &browser, | 225 &browser, |
| 196 NULL, | 226 NULL, |
| 197 &web_contents, | 227 &web_contents, |
| 198 NULL)) { | 228 NULL)) { |
| 199 return NULL; | 229 return NULL; |
| 200 } | 230 } |
| 201 return web_contents; | 231 return web_contents; |
| 202 } | 232 } |
| 233 | |
| 234 void PageCaptureSaveAsMHTMLFunction::HandlePermission() { | |
| 235 #if defined(OS_CHROMEOS) | |
| 236 auto user_choice = UserChoiceGet(); | |
| 237 if (user_choice == PermissionState::ALLOWED || | |
| 238 user_choice == PermissionState::DENIED) { | |
| 239 ChainRunAsync(); | |
| 240 return; | |
| 241 } | |
| 242 if (user_choice == PermissionState::NOT_PROMPTED) { | |
| 243 ShowPermissionPrompt(); | |
| 244 } | |
| 245 // It's possible that several PageCapture requests come before user resolves | |
| 246 // the permission dialog, so setup a callback that gets called when pref | |
| 247 // changes (dialog is resolved). | |
| 248 pref_change_registrar_.reset(new PrefChangeRegistrar); | |
| 249 pref_change_registrar_->Init(GetPrefs()); | |
| 250 // base::Unretained is safe here because PrefChangeRegistrar will unregister | |
| 251 // callback on destruction of this. | |
|
Devlin
2016/12/12 20:09:37
What if this object outlives the Prefs?
Ivan Šandrk
2016/12/13 16:32:47
Prefs are destoyed during logout. This object eith
| |
| 252 pref_change_registrar_->Add(prefs::kPublicSessionPermissionsUserChoiceCache, | |
| 253 base::Bind(&PageCaptureSaveAsMHTMLFunction::ChainRunAsync, | |
| 254 base::Unretained(this))); | |
| 255 #endif // defined(OS_CHROMEOS) | |
| 256 } | |
| 257 | |
| 258 #if defined(OS_CHROMEOS) | |
| 259 void PageCaptureSaveAsMHTMLFunction::ShowPermissionPrompt() { | |
| 260 extensions::APIPermissionSet new_apis; | |
| 261 new_apis.insert(extensions::APIPermission::kPageCapture); | |
| 262 auto permission_set = base::MakeUnique<extensions::PermissionSet>( | |
| 263 new_apis, extensions::ManifestPermissionSet(), | |
| 264 extensions::URLPatternSet(), extensions::URLPatternSet()); | |
| 265 | |
| 266 prompt_ = base::MakeUnique<ExtensionInstallPrompt>(GetWebContents()); | |
|
Devlin
2016/12/12 20:09:37
GetWebContents() can return null here, which would
Ivan Šandrk
2016/12/13 16:32:47
Good catch. Replaced it with GetAssociatedWebConte
Devlin
2016/12/13 21:51:29
GetAssociatedWebContents() isn't particularly appr
Ivan Šandrk
2017/01/04 17:09:59
I'd personally show the permission dialog anyway,
| |
| 267 prompt_->ShowDialog( | |
| 268 base::Bind(&PageCaptureSaveAsMHTMLFunction::ResolvePermissionPrompt, | |
| 269 this), | |
| 270 extension(), | |
| 271 nullptr, // Uses the extension icon. | |
| 272 base::MakeUnique<ExtensionInstallPrompt::Prompt>( | |
| 273 ExtensionInstallPrompt::PERMISSIONS_PROMPT), | |
| 274 std::move(permission_set), | |
| 275 ExtensionInstallPrompt::GetDefaultShowDialogCallback()); | |
| 276 | |
| 277 UserChoiceSet(PermissionState::SHOWN_PROMPT); | |
| 278 } | |
| 279 | |
| 280 void PageCaptureSaveAsMHTMLFunction::ResolvePermissionPrompt( | |
| 281 ExtensionInstallPrompt::Result prompt_result) { | |
| 282 // Dispose of the prompt as it's not needed anymore. | |
| 283 prompt_.reset(); | |
| 284 | |
| 285 bool allowed = prompt_result == ExtensionInstallPrompt::Result::ACCEPTED; | |
| 286 UserChoiceSet(allowed ? PermissionState::ALLOWED : PermissionState::DENIED); | |
| 287 } | |
| 288 | |
| 289 void PageCaptureSaveAsMHTMLFunction::ChainRunAsync() { | |
| 290 auto user_choice = UserChoiceGet(); | |
| 291 // Permission was allowed/denied for some other extension, continue sleeping. | |
| 292 if (user_choice == PermissionState::SHOWN_PROMPT) { | |
| 293 return; | |
| 294 } | |
| 295 | |
| 296 if (user_choice == PermissionState::DENIED) { | |
| 297 ReturnFailure(kUserDenied); | |
| 298 } else { | |
| 299 DCHECK(user_choice == PermissionState::ALLOWED); | |
| 300 BrowserThread::PostTask( | |
| 301 BrowserThread::FILE, FROM_HERE, | |
| 302 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 PrefService* PageCaptureSaveAsMHTMLFunction::GetPrefs() { | |
| 307 return GetProfile()->GetOffTheRecordPrefs(); | |
| 308 } | |
| 309 | |
| 310 void PageCaptureSaveAsMHTMLFunction::UserChoiceSet(PermissionState value) { | |
| 311 DictionaryPrefUpdate update(GetPrefs(), | |
| 312 prefs::kPublicSessionPermissionsUserChoiceCache); | |
| 313 update->SetInteger(extension()->id(), value); | |
| 314 } | |
| 315 | |
| 316 PageCaptureSaveAsMHTMLFunction::PermissionState | |
| 317 PageCaptureSaveAsMHTMLFunction::UserChoiceGet() { | |
| 318 auto dictionary = GetPrefs()->GetDictionary( | |
| 319 prefs::kPublicSessionPermissionsUserChoiceCache); | |
| 320 int value; | |
| 321 if (!dictionary->GetInteger(extension()->id(), &value)) { | |
| 322 value = PermissionState::NOT_PROMPTED; | |
| 323 UserChoiceSet(static_cast<PermissionState>(value)); | |
| 324 } | |
| 325 return static_cast<PermissionState>(value); | |
| 326 } | |
| 327 #endif // defined(OS_CHROMEOS) | |
| OLD | NEW |