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 kNoAvailableBrowser[] = | |
| 49 "No available browser window to show the prompt."; | |
| 50 const char kUserDenied[] = "User denied request."; | |
| 51 #endif | |
| 52 | |
| 53 bool IsPublicSession() { | |
|
Andrew T Wilson (Slow)
2016/12/14 13:39:21
Seems like this should be in ifdef(OS_CHROMEOS), a
Ivan Šandrk
2017/01/04 17:09:59
Done.
| |
| 54 #if defined(OS_CHROMEOS) | |
| 55 if (chromeos::LoginState::IsInitialized()) | |
| 56 return chromeos::LoginState::Get()->IsPublicSessionUser(); | |
| 57 #endif | |
| 58 return false; | |
| 59 } | |
| 60 | |
| 40 } // namespace | 61 } // namespace |
| 41 | 62 |
| 42 static PageCaptureSaveAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; | 63 static PageCaptureSaveAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; |
| 43 | 64 |
| 44 PageCaptureSaveAsMHTMLFunction::PageCaptureSaveAsMHTMLFunction() { | 65 PageCaptureSaveAsMHTMLFunction::PageCaptureSaveAsMHTMLFunction() { |
| 45 } | 66 } |
| 46 | 67 |
| 47 PageCaptureSaveAsMHTMLFunction::~PageCaptureSaveAsMHTMLFunction() { | 68 PageCaptureSaveAsMHTMLFunction::~PageCaptureSaveAsMHTMLFunction() { |
| 48 if (mhtml_file_.get()) { | 69 if (mhtml_file_.get()) { |
| 49 storage::ShareableFileReference* to_release = mhtml_file_.get(); | 70 storage::ShareableFileReference* to_release = mhtml_file_.get(); |
| 50 to_release->AddRef(); | 71 to_release->AddRef(); |
| 51 mhtml_file_ = NULL; | 72 mhtml_file_ = NULL; |
| 52 BrowserThread::ReleaseSoon(BrowserThread::IO, FROM_HERE, to_release); | 73 BrowserThread::ReleaseSoon(BrowserThread::IO, FROM_HERE, to_release); |
| 53 } | 74 } |
| 54 } | 75 } |
| 55 | 76 |
| 56 void PageCaptureSaveAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { | 77 void PageCaptureSaveAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { |
| 57 test_delegate_ = delegate; | 78 test_delegate_ = delegate; |
| 58 } | 79 } |
| 59 | 80 |
| 60 bool PageCaptureSaveAsMHTMLFunction::RunAsync() { | 81 bool PageCaptureSaveAsMHTMLFunction::RunAsync() { |
| 61 params_ = SaveAsMHTML::Params::Create(*args_); | 82 params_ = SaveAsMHTML::Params::Create(*args_); |
| 62 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 83 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 63 | 84 |
| 64 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() | 85 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() |
| 65 | 86 |
| 87 // In Public Sessions, extensions (and apps) are force-installed by admin | |
| 88 // policy so the user does not get a chance to review the permissions for | |
| 89 // these extensions. This is not acceptable from a security/privacy | |
| 90 // standpoint, so when an extension uses the PageCapture API for the first | |
| 91 // time, we show the user a dialog where they can choose whether to allow the | |
| 92 // extension access to the API. | |
| 93 if (IsPublicSession()) { | |
| 94 HandlePermissionRequest(); | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 66 BrowserThread::PostTask( | 98 BrowserThread::PostTask( |
| 67 BrowserThread::FILE, FROM_HERE, | 99 BrowserThread::FILE, FROM_HERE, |
| 68 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); | 100 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); |
| 69 return true; | 101 return true; |
| 70 } | 102 } |
| 71 | 103 |
| 72 bool PageCaptureSaveAsMHTMLFunction::OnMessageReceived( | 104 bool PageCaptureSaveAsMHTMLFunction::OnMessageReceived( |
| 73 const IPC::Message& message) { | 105 const IPC::Message& message) { |
| 74 if (message.type() != ExtensionHostMsg_ResponseAck::ID) | 106 if (message.type() != ExtensionHostMsg_ResponseAck::ID) |
| 75 return false; | 107 return false; |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 GetProfile(), | 225 GetProfile(), |
| 194 include_incognito(), | 226 include_incognito(), |
| 195 &browser, | 227 &browser, |
| 196 NULL, | 228 NULL, |
| 197 &web_contents, | 229 &web_contents, |
| 198 NULL)) { | 230 NULL)) { |
| 199 return NULL; | 231 return NULL; |
| 200 } | 232 } |
| 201 return web_contents; | 233 return web_contents; |
| 202 } | 234 } |
| 235 | |
| 236 void PageCaptureSaveAsMHTMLFunction::HandlePermissionRequest() { | |
|
Devlin
2016/12/13 21:51:29
It's kind of weird that we define this function fo
Ivan Šandrk
2017/01/04 17:09:59
Wanted to see how this variant fared - the regular
| |
| 237 #if defined(OS_CHROMEOS) | |
| 238 auto user_choice = UserChoiceGet(); | |
| 239 if (user_choice == PermissionState::ALLOWED || | |
| 240 user_choice == PermissionState::DENIED) { | |
| 241 ResolvePermissionRequest(); | |
| 242 return; | |
| 243 } | |
| 244 if (user_choice == PermissionState::NOT_PROMPTED) { | |
| 245 ShowPermissionPrompt(); | |
| 246 } | |
| 247 // It's possible that several PageCapture requests come before user resolves | |
| 248 // the permission dialog, so setup a callback that gets called when pref | |
| 249 // changes (dialog is resolved). | |
| 250 pref_change_registrar_.reset(new PrefChangeRegistrar); | |
| 251 pref_change_registrar_->Init(GetPrefs()); | |
| 252 // base::Unretained is safe here because PrefChangeRegistrar will unregister | |
| 253 // callback on destruction of this. | |
|
Andrew T Wilson (Slow)
2016/12/14 13:39:21
nit: this object
Ivan Šandrk
2017/01/04 17:09:59
Done.
| |
| 254 pref_change_registrar_->Add(prefs::kPublicSessionPermissionsUserChoiceCache, | |
|
Devlin
2016/12/13 21:51:29
Is this git cl format'd?
Ivan Šandrk
2017/01/04 17:09:59
Done
| |
| 255 base::Bind(&PageCaptureSaveAsMHTMLFunction::ResolvePermissionRequest, | |
| 256 base::Unretained(this))); | |
| 257 #endif // defined(OS_CHROMEOS) | |
| 258 } | |
| 259 | |
| 260 #if defined(OS_CHROMEOS) | |
| 261 void PageCaptureSaveAsMHTMLFunction::ShowPermissionPrompt() { | |
| 262 extensions::APIPermissionSet new_apis; | |
| 263 new_apis.insert(extensions::APIPermission::kPageCapture); | |
| 264 auto permission_set = base::MakeUnique<extensions::PermissionSet>( | |
| 265 new_apis, extensions::ManifestPermissionSet(), | |
| 266 extensions::URLPatternSet(), extensions::URLPatternSet()); | |
| 267 | |
| 268 auto web_contents = GetAssociatedWebContents(); | |
| 269 if (!web_contents) { | |
| 270 ReturnFailure(kNoAvailableBrowser); | |
| 271 return; | |
| 272 } | |
| 273 prompt_ = base::MakeUnique<ExtensionInstallPrompt>(web_contents); | |
| 274 prompt_->ShowDialog( | |
| 275 base::Bind(&PageCaptureSaveAsMHTMLFunction::ResolvePermissionPrompt, | |
| 276 this), | |
| 277 extension(), | |
| 278 nullptr, // Uses the extension icon. | |
| 279 base::MakeUnique<ExtensionInstallPrompt::Prompt>( | |
| 280 ExtensionInstallPrompt::PERMISSIONS_PROMPT), | |
| 281 std::move(permission_set), | |
| 282 ExtensionInstallPrompt::GetDefaultShowDialogCallback()); | |
| 283 | |
| 284 UserChoiceSet(PermissionState::SHOWN_PROMPT); | |
| 285 } | |
| 286 | |
| 287 void PageCaptureSaveAsMHTMLFunction::ResolvePermissionPrompt( | |
| 288 ExtensionInstallPrompt::Result prompt_result) { | |
| 289 // Dispose of the prompt as it's not needed anymore. | |
| 290 prompt_.reset(); | |
| 291 | |
| 292 bool allowed = prompt_result == ExtensionInstallPrompt::Result::ACCEPTED; | |
| 293 UserChoiceSet(allowed ? PermissionState::ALLOWED : PermissionState::DENIED); | |
| 294 } | |
| 295 | |
| 296 void PageCaptureSaveAsMHTMLFunction::ResolvePermissionRequest() { | |
| 297 auto user_choice = UserChoiceGet(); | |
| 298 // Permission was allowed/denied for some other extension, continue sleeping. | |
| 299 if (user_choice == PermissionState::SHOWN_PROMPT) { | |
| 300 return; | |
| 301 } | |
| 302 | |
| 303 if (user_choice == PermissionState::DENIED) { | |
| 304 ReturnFailure(kUserDenied); | |
| 305 } else { | |
| 306 DCHECK(user_choice == PermissionState::ALLOWED); | |
| 307 BrowserThread::PostTask( | |
| 308 BrowserThread::FILE, FROM_HERE, | |
| 309 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); | |
| 310 } | |
| 311 } | |
| 312 | |
| 313 PrefService* PageCaptureSaveAsMHTMLFunction::GetPrefs() { | |
| 314 return GetProfile()->GetOffTheRecordPrefs(); | |
|
Devlin
2016/12/13 21:51:29
Why OffTheRecordPrefs()? (add a comment)
Ivan Šandrk
2017/01/04 17:09:59
Done. It's used because the pref must not be persi
| |
| 315 } | |
| 316 | |
| 317 void PageCaptureSaveAsMHTMLFunction::UserChoiceSet(PermissionState value) { | |
| 318 DictionaryPrefUpdate update(GetPrefs(), | |
| 319 prefs::kPublicSessionPermissionsUserChoiceCache); | |
| 320 update->SetInteger(extension()->id(), value); | |
| 321 } | |
| 322 | |
| 323 PageCaptureSaveAsMHTMLFunction::PermissionState | |
| 324 PageCaptureSaveAsMHTMLFunction::UserChoiceGet() { | |
| 325 auto dictionary = GetPrefs()->GetDictionary( | |
| 326 prefs::kPublicSessionPermissionsUserChoiceCache); | |
| 327 int value; | |
|
Devlin
2016/12/13 21:51:29
nit: initialize value
Ivan Šandrk
2017/01/04 17:09:59
Done.
| |
| 328 if (!dictionary->GetInteger(extension()->id(), &value)) { | |
| 329 value = PermissionState::NOT_PROMPTED; | |
| 330 UserChoiceSet(static_cast<PermissionState>(value)); | |
|
Andrew T Wilson (Slow)
2016/12/14 13:39:20
Why do we need to call UserChoiceSet here? Why not
Ivan Šandrk
2017/01/04 17:09:59
Done.
| |
| 331 } | |
| 332 return static_cast<PermissionState>(value); | |
| 333 } | |
| 334 #endif // defined(OS_CHROMEOS) | |
| OLD | NEW |