Home | History | Annotate | Download | only in app_runtime
      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
      3 // found in the LICENSE file.
      4 
      5 #include "apps/browser/api/app_runtime/app_runtime_api.h"
      6 
      7 #include "apps/browser/file_handler_util.h"
      8 #include "apps/common/api/app_runtime.h"
      9 #include "base/time/time.h"
     10 #include "base/values.h"
     11 #include "extensions/browser/event_router.h"
     12 #include "extensions/browser/extension_prefs.h"
     13 #include "extensions/browser/extensions_browser_client.h"
     14 #include "url/gurl.h"
     15 
     16 using content::BrowserContext;
     17 using extensions::Event;
     18 using extensions::Extension;
     19 using extensions::ExtensionPrefs;
     20 
     21 namespace apps {
     22 
     23 namespace app_runtime = api::app_runtime;
     24 
     25 namespace {
     26 
     27 void DispatchOnLaunchedEventImpl(const std::string& extension_id,
     28                                  scoped_ptr<base::DictionaryValue> launch_data,
     29                                  BrowserContext* context) {
     30   // "Forced app mode" is true for Chrome OS kiosk mode.
     31   launch_data->SetBoolean(
     32       "isKioskSession",
     33       extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode());
     34   scoped_ptr<base::ListValue> args(new base::ListValue());
     35   args->Append(launch_data.release());
     36   scoped_ptr<Event> event(
     37       new Event(app_runtime::OnLaunched::kEventName, args.Pass()));
     38   event->restrict_to_browser_context = context;
     39   event->can_load_ephemeral_apps = true;
     40   extensions::EventRouter::Get(context)
     41       ->DispatchEventWithLazyListener(extension_id, event.Pass());
     42   ExtensionPrefs::Get(context)
     43       ->SetLastLaunchTime(extension_id, base::Time::Now());
     44 }
     45 
     46 }  // anonymous namespace
     47 
     48 // static.
     49 void AppEventRouter::DispatchOnLaunchedEvent(BrowserContext* context,
     50                                              const Extension* extension) {
     51   scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue());
     52   DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context);
     53 }
     54 
     55 // static.
     56 void AppEventRouter::DispatchOnRestartedEvent(BrowserContext* context,
     57                                               const Extension* extension) {
     58   scoped_ptr<base::ListValue> arguments(new base::ListValue());
     59   scoped_ptr<Event> event(
     60       new Event(app_runtime::OnRestarted::kEventName, arguments.Pass()));
     61   event->restrict_to_browser_context = context;
     62   event->can_load_ephemeral_apps = true;
     63   extensions::EventRouter::Get(context)
     64       ->DispatchEventToExtension(extension->id(), event.Pass());
     65 }
     66 
     67 // static.
     68 void AppEventRouter::DispatchOnLaunchedEventWithFileEntries(
     69     BrowserContext* context,
     70     const Extension* extension,
     71     const std::string& handler_id,
     72     const std::vector<std::string>& mime_types,
     73     const std::vector<file_handler_util::GrantedFileEntry>& file_entries) {
     74   // TODO(sergeygs): Use the same way of creating an event (using the generated
     75   // boilerplate) as below in DispatchOnLaunchedEventWithUrl.
     76   scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue);
     77   launch_data->SetString("id", handler_id);
     78   scoped_ptr<base::ListValue> items(new base::ListValue);
     79   DCHECK(file_entries.size() == mime_types.size());
     80   for (size_t i = 0; i < file_entries.size(); ++i) {
     81     scoped_ptr<base::DictionaryValue> launch_item(new base::DictionaryValue);
     82     launch_item->SetString("fileSystemId", file_entries[i].filesystem_id);
     83     launch_item->SetString("baseName", file_entries[i].registered_name);
     84     launch_item->SetString("mimeType", mime_types[i]);
     85     launch_item->SetString("entryId", file_entries[i].id);
     86     items->Append(launch_item.release());
     87   }
     88   launch_data->Set("items", items.release());
     89   DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context);
     90 }
     91 
     92 // static.
     93 void AppEventRouter::DispatchOnLaunchedEventWithUrl(
     94     BrowserContext* context,
     95     const Extension* extension,
     96     const std::string& handler_id,
     97     const GURL& url,
     98     const GURL& referrer_url) {
     99   api::app_runtime::LaunchData launch_data;
    100   launch_data.id.reset(new std::string(handler_id));
    101   launch_data.url.reset(new std::string(url.spec()));
    102   launch_data.referrer_url.reset(new std::string(referrer_url.spec()));
    103   DispatchOnLaunchedEventImpl(
    104       extension->id(), launch_data.ToValue().Pass(), context);
    105 }
    106 
    107 }  // namespace apps
    108