Chromium Code Reviews| 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 "chrome/browser/apps/install_chrome_app.h" | 5 #include "chrome/browser/apps/install_chrome_app.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/extensions/webstore_startup_installer.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/ui/browser.h" | |
| 11 #include "chrome/browser/ui/browser_list.h" | 14 #include "chrome/browser/ui/browser_list.h" |
| 12 #include "chrome/browser/ui/browser_navigator.h" | 15 #include "chrome/common/extensions/webstore_install_result.h" |
|
tapted
2014/07/28 04:35:43
meta-nit: with my nit below, you probably won't ne
jackhou1
2014/07/28 05:16:35
Done.
| |
| 16 #include "extensions/browser/extension_registry.h" | |
| 13 #include "extensions/common/extension.h" | 17 #include "extensions/common/extension.h" |
| 14 #include "google_apis/gaia/gaia_urls.h" | 18 |
| 15 #include "net/url_request/url_fetcher.h" | 19 using extensions::ExtensionRegistry; |
| 16 #include "net/url_request/url_fetcher_delegate.h" | |
| 17 | 20 |
| 18 namespace { | 21 namespace { |
| 19 | 22 |
| 20 // The URL to the webstore page for a specific app. "_asi=1" instructs webstore | 23 // The URL to the webstore page for a specific app. |
| 21 // to immediately try to install the app if the referrer is the sign in page. | |
| 22 // This is actually the short form of the URL which just redirects to the full | |
| 23 // URL. Since "_asi=1" only works on the full url, we need to resolve it first | |
| 24 // before navigating the user to it. | |
| 25 const char kWebstoreUrlFormat[] = | 24 const char kWebstoreUrlFormat[] = |
| 26 "https://chrome.google.com/webstore/detail/%s?_asi=1"; | 25 "https://chrome.google.com/webstore/detail/%s"; |
| 27 | |
| 28 // The URL for the sign in page, set as the referrer to webstore. | |
| 29 const char kAccountsUrl[] = "https://accounts.google.com/ServiceLogin"; | |
| 30 | 26 |
| 31 // Returns the webstore URL for an app. | 27 // Returns the webstore URL for an app. |
| 32 GURL GetAppInstallUrl(const std::string& app_id) { | 28 GURL GetAppInstallUrl(const std::string& app_id) { |
| 33 return GURL(base::StringPrintf(kWebstoreUrlFormat, app_id.c_str())); | 29 return GURL(base::StringPrintf(kWebstoreUrlFormat, app_id.c_str())); |
| 34 } | 30 } |
| 35 | 31 |
| 36 void NavigateToUrlWithAccountsReferrer(const GURL& url) { | 32 void DoNothingCallback(bool success, |
| 37 Browser* browser = | 33 const std::string& error, |
| 38 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE)->get(0); | 34 extensions::webstore_install::Result result) { |
| 39 if (!browser) | |
| 40 return; | |
| 41 | |
| 42 chrome::NavigateParams params( | |
| 43 browser, url, content::PAGE_TRANSITION_AUTO_TOPLEVEL); | |
| 44 params.disposition = NEW_FOREGROUND_TAB; | |
| 45 params.window_action = chrome::NavigateParams::SHOW_WINDOW; | |
| 46 params.referrer = content::Referrer(); | |
| 47 params.referrer.url = GURL(kAccountsUrl); | |
| 48 chrome::Navigate(¶ms); | |
| 49 } | |
| 50 | |
| 51 class AppURLFetcher : net::URLFetcherDelegate { | |
| 52 public: | |
| 53 explicit AppURLFetcher(const std::string& app_id); | |
| 54 | |
| 55 // net::URLFetcherDelegate OVERRIDES: | |
| 56 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 57 | |
| 58 private: | |
| 59 virtual ~AppURLFetcher(); | |
| 60 | |
| 61 scoped_ptr<net::URLFetcher> url_fetcher_; | |
| 62 | |
| 63 DISALLOW_COPY_AND_ASSIGN(AppURLFetcher); | |
| 64 }; | |
| 65 | |
| 66 AppURLFetcher::AppURLFetcher(const std::string& app_id) { | |
| 67 url_fetcher_.reset(net::URLFetcher::Create( | |
| 68 GetAppInstallUrl(app_id), net::URLFetcher::GET, this)); | |
| 69 url_fetcher_->SetRequestContext(g_browser_process->system_request_context()); | |
| 70 url_fetcher_->SetStopOnRedirect(true); | |
| 71 url_fetcher_->Start(); | |
| 72 } | |
| 73 | |
| 74 AppURLFetcher::~AppURLFetcher() { | |
| 75 } | |
| 76 | |
| 77 void AppURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 78 if (source->GetResponseCode() == 301) { | |
| 79 // Moved permanently. | |
| 80 NavigateToUrlWithAccountsReferrer(source->GetURL()); | |
| 81 } | |
| 82 | |
| 83 delete this; | |
| 84 } | 35 } |
| 85 | 36 |
| 86 } // namespace | 37 } // namespace |
| 87 | 38 |
| 88 namespace install_chrome_app { | 39 namespace install_chrome_app { |
| 89 | 40 |
| 90 void InstallChromeApp(const std::string& app_id) { | 41 void InstallChromeApp(const std::string& app_id) { |
| 91 if (!extensions::Extension::IdIsValid(app_id)) | 42 if (!extensions::Extension::IdIsValid(app_id)) |
| 92 return; | 43 return; |
| 93 | 44 |
| 94 new AppURLFetcher(app_id); | 45 // At the moment InstallChromeApp() is called immediately after handling |
| 46 // startup URLs, so a browser is guaranteed to be created. If that changes we | |
| 47 // may need to start a browser or browser session here. | |
| 48 Browser* browser = | |
| 49 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE)->get(0); | |
| 50 if (!browser) | |
|
tapted
2014/07/28 04:35:43
nit: DCHECK(browser) - (if there's a null pointer
jackhou1
2014/07/28 05:16:35
Done.
| |
| 51 return; | |
| 52 | |
| 53 content::OpenURLParams params(GetAppInstallUrl(app_id), | |
| 54 content::Referrer(), | |
| 55 NEW_FOREGROUND_TAB, | |
| 56 content::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
| 57 false); | |
| 58 browser->OpenURL(params); | |
| 59 | |
| 60 ExtensionRegistry* registry = ExtensionRegistry::Get(browser->profile()); | |
| 61 // Skip if this app is already installed or blacklisted. For disabled or | |
| 62 // or terminated apps, going through the installation flow should re-enable | |
| 63 // them. | |
| 64 const extensions::Extension* installed_extension = registry->GetExtensionById( | |
| 65 app_id, ExtensionRegistry::ENABLED | ExtensionRegistry::BLACKLISTED); | |
| 66 // TODO(jackhou): For installed apps, maybe we should do something better, | |
| 67 // e.g. show the app list (and re-add it to the taskbar). | |
| 68 if (installed_extension) | |
| 69 return; | |
| 70 | |
| 71 extensions::WebstoreStartupInstaller* installer = | |
| 72 new extensions::WebstoreStartupInstaller( | |
|
tapted
2014/07/28 04:35:43
Not sure if you want this, but I think you could m
jackhou1
2014/07/28 05:16:35
Done.
| |
| 73 app_id, browser->profile(), true, base::Bind(&DoNothingCallback)); | |
|
tapted
2014/07/28 04:35:43
nit: probably nicer to do
`base::Bind(&DoNothingC
jackhou1
2014/07/28 05:16:35
Done.
| |
| 74 installer->BeginInstall(); | |
| 95 } | 75 } |
| 96 | 76 |
| 97 } // namespace install_chrome_app | 77 } // namespace install_chrome_app |
| OLD | NEW |